package com.chuantou.yuba.ui.fragment;

import java.util.HashMap;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.chuantou.yuba.R;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.BaseGsonRequest;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.TrendsDataTO;
import com.chuantou.yuba.util.Constants;

public class FollowTrendsFragment extends TrendsFragment {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext.registerReceiver(mFollowChangeReceiver, new IntentFilter(Constants.INTENT_ACITON_FRIEND_CHANGED));
	}

	@Override
	protected BaseGsonRequest<TrendsDataTO> getRequest(Listener<TrendsDataTO> listener, ErrorListener errorListener) {
		HashMap<String, String> params = new HashMap<String, String>();
		String token = mUser != null ? mUser.getToken() : null;
		long uid = mUser != null ? mUser.getUid() : 0;
		if (!TextUtils.isEmpty(token)) {
			params.put(UriHelper.PARAMS_TOKEN, token);
		}
		if (uid > 0) {
			params.put(UriHelper.PARAMS_UID, String.valueOf(uid));
		}
		params.put(UriHelper.PARAMS_SHOW, "collection");
		params.put(UriHelper.PARAMS_PAGE_NO, String.valueOf(1));
		params.put(UriHelper.PARAMS_PAGE_SIZE, String.valueOf(Constants.DEFAULT_PAGE_SIZE));
		String requestUrl = UriHelper.getTrendsListUrl();
		return new GsonRequest<TrendsDataTO>(Method.POST, requestUrl, listener, errorListener, params, TrendsDataTO.class);
	}

	@Override
	protected String getCacheKey() {
		return TRENDS_CACHE_PREFIX.concat("followed");
	}

	@Override
	protected String getEmptyString() {
		return mContext.getString(R.string.no_follow_trends_content);
	}

	private BroadcastReceiver mFollowChangeReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			load(1);
		}
	};

	public void onDestroy() {
		mContext.unregisterReceiver(mFollowChangeReceiver);
		super.onDestroy();
	};
}
